package com.zhengdao.video;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WangHao on 2020/3/10.
 * E-Mail: wh_main@163.com
 * Description:
 */
public class AlbumUtils {

    public static List<VideoBean> getList(Context context) {
        List<VideoBean> list = null;
        if (context != null) {
//            String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
//                    MediaStore.Video.Thumbnails.VIDEO_ID};
//            Cursor cursor = context.getContentResolver().query(
//                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
//                    null, null);


            String[] projection = {
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.DISPLAY_NAME,
                    MediaStore.Video.Media.DURATION,
                    MediaStore.Video.Media.SIZE
            };
            //全部图片
            String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=? or "
                    + MediaStore.Video.Media.MIME_TYPE + "=?";
            String[] whereArgs = {"video/mp4", "video/3gp", "video/aiv", "video/rmvb", "video/vob", "video/flv",
                    "video/mkv", "video/mov", "video/mpg"};

            Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    null, where, whereArgs, MediaStore.Video.Media.DATE_ADDED + " DESC ");


            if (cursor != null) {
                list = new ArrayList<VideoBean>();
                while (cursor.moveToNext()) {
                    int id = cursor.getInt(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                    String title = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                    String album = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));
                    String artist = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
                    String displayName = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
                    String mimeType = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                    String path = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                    long duration = cursor
                            .getInt(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                    long size = cursor
                            .getLong(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));

                    //视频缩略图路径
                    String albumPath = "";
                    Cursor thumbCursor = context.getApplicationContext().getContentResolver().query(
                            MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                            null, MediaStore.Video.Thumbnails.VIDEO_ID
                                    + "=" + id, null, null);
                    if (thumbCursor.moveToFirst()) {
                        albumPath = thumbCursor.getString(thumbCursor
                                .getColumnIndex(MediaStore.Video.Thumbnails.DATA));

                    }
                    VideoBean video = new VideoBean(title,size,duration,path,albumPath);
                    list.add(video);
                }
                cursor.close();
            }
        }

        return list;
    }
}
